package live.jipiao.dao;

import java.io.IOException;
import java.util.List;

import live.common.AirParam;
import live.common.Format;
import live.jipiao.entity.AirQuery;
import live.jipiao.entity.FlightItem;
import live.jipiao.entity.PnrInfo;
import live.jipiao.entity.RateInfos;
public class FlightCommon {
	private StringBuffer buffer = null;
	private Format format = null;

	/**
	 * 组装航班信息
	 * 
	 * @return 航班信息串
	 */
	public String returnFlightInfo(AirQuery query,
			List<FlightItem> chooseFlights) {
		FlightItem formFlight = chooseFlights.get(0);
		format = new Format();
		buffer = new StringBuffer(query.getFromCity()).append("^").append(
				query.getToCity()).append("^").append(
				format.dateFormatString(formFlight.getDepTime(),
						"yyyy-MM-dd HH:mm")).append("^").append(
				format.dateFormatString(formFlight.getArrTime(),
						"yyyy-MM-dd HH:mm")).append("^").append(
				formFlight.getCabinInfo().getCode()).append("^").append(
				formFlight.getFlightNo()).append("^").append(
				formFlight.getModel()).append("^").append(
				formFlight.getCabinInfo().getDiscount()).append("^").append(0);
		if (query.getVoyageType() == 2) {
			FlightItem toFlight = chooseFlights.get(0);
			buffer.append("|").append(query.getToCity()).append("^").append(
					query.getFromCity()).append("^").append(
					format.dateFormatString(toFlight.getDepTime(),
							"yyyy-MM-dd HH:mm")).append("^").append(
					format.dateFormatString(toFlight.getArrTime(),
							"yyyy-MM-dd HH:mm")).append("^").append(
					toFlight.getCabinInfo().getCode()).append("^").append(
					toFlight.getFlightNo()).append("^").append(
					toFlight.getModel()).append("^").append(
					toFlight.getCabinInfo().getDiscount()).append("^")
					.append(1);
		}
		return buffer.toString();
	}

	public String returnFlightInfoForPnr(AirQuery query,
			List<FlightItem> chooseFlights) {
		FlightItem formFlight = chooseFlights.get(0);
		buffer = new StringBuffer(query.getFromCity()).append("^").append(
				query.getToCity()).append("^").append(query.getFromDate())
				.append("^").append(formFlight.getCabinInfo().getCode())
				.append("^").append(formFlight.getFlightNo());
		if (query.getVoyageType() == 2) {
			FlightItem toFlight = chooseFlights.get(0);
			buffer.append("|").append(query.getToCity()).append("^").append(
					query.getFromCity()).append("^").append(query.getToDate())
					.append("^").append(toFlight.getCabinInfo().getCode())
					.append("^").append(toFlight.getFlightNo());
		}
		return buffer.toString();
	}

	/**
	 * 组装乘客信息
	 * 
	 * @return 乘客信息串
	 */
	/*
	 * public String returnPassenagerInfo(){
	 * 
	 * return null; }
	 */

	/**
	 * 组装PNR信息
	 * 
	 * @return PNR 信息串
	 */
	public String returnPnrInfo(PnrInfo item) {
		buffer = new StringBuffer(item.getCode()).append("^").append(
				item.getBigCode()).append("^").append(item.getTeamType())
				.append("^").append(item.getRouteType()).append("^").append(
						Integer.parseInt(item.getFare())).append("^").append(
								Integer.parseInt(item.getTax())
								+ Integer.parseInt(item.getYq())).append("^")
				.append(item.getOfficeNo());
		return buffer.toString();
	}

	/**
	 * 组装订单信息
	 * 
	 * @return 订单信息串
	 * @throws IOException
	 */
	public String returnOrderInfo(List<RateInfos> rates) {
		String partner = AirParam.AIR_PARTNER;
		buffer = new StringBuffer();
		RateInfos item = null;
		for (int i = 0; i < rates.size(); i++) {
			item = rates.get(i);
			buffer.append("0^").append(partner).append("^").append(partner)
					.append("^").append(item.getRateNo()).append("^").append(
							item.getDisCount()).append("^").append(0).append(
							"^").append(item.getAdultsType());
			if (i != rates.size() - 1) {
				buffer.append("|");
			}
			item = null;
		}
		return buffer.toString();
	}
}
